package com.zy.test.widget

import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.Typeface
import android.os.Bundle
import android.view.WindowManager
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.zy.activity.BaseActivity
import com.zy.test.widget.fragment.TabLayoutFragment


//ViewPager2+TabLayout
class TabLayoutTest1Activity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN
        )

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_tab_layout_test)
        initView()
    }

    private val activeColor: Int = Color.parseColor("#ff678f")
    private val normalColor: Int = Color.parseColor("#666666")

    private val activeSize = 20F
    private val normalSize = 14F


    private lateinit var viewPager2: ViewPager2
    private lateinit var tabLayout: TabLayout

    private var mediator: TabLayoutMediator? = null

    private fun initView() {
        tabLayout = findViewById<TabLayout>(R.id.tab_layout)
        viewPager2 = findViewById<ViewPager2>(R.id.view_pager)

        val tabs = arrayOf("关注", "推荐", "最新0", "最新1", "最新2", "最新3", "最新4", "最新5", "最新6")

        //禁用预加载
        viewPager2.offscreenPageLimit = ViewPager2.OFFSCREEN_PAGE_LIMIT_DEFAULT;

        //Adapter
        //Adapter
        viewPager2.adapter = object : FragmentStateAdapter(supportFragmentManager, lifecycle) {
            override fun createFragment(position: Int): Fragment {
                //FragmentStateAdapter内部自己会管理已实例化的fragment对象。
                // 所以不需要考虑复用的问题
                return TabLayoutFragment.newInstance(tabs[position])
            }

            override fun getItemCount(): Int {
                return tabs.size
            }
        }

        //viewPager 页面切换监听监听
        viewPager2.registerOnPageChangeCallback(changeCallback);

        mediator = TabLayoutMediator(tabLayout, viewPager2) { tab, position -> //这里可以自定义TabView
            val tabView = TextView(this@TabLayoutTest1Activity)
            val states = arrayOfNulls<IntArray>(2)
            states[0] = intArrayOf(android.R.attr.state_selected)
            states[1] = intArrayOf()
            val colors = intArrayOf(activeColor, normalColor)
            val colorStateList = ColorStateList(states, colors)
            tabView.text = tabs[position]
            tabView.textSize = normalSize
            tabView.setTextColor(colorStateList)

            tab.customView = tabView
        }

        //要执行这一句才是真正将两者绑定起来
        mediator?.attach();
    }


    private val changeCallback: OnPageChangeCallback = object : OnPageChangeCallback() {
        override fun onPageSelected(position: Int) {
            tabLayout.let {
                //可以来设置选中时tab的大小
                val tabCount: Int = it.tabCount
                for (i in 0 until tabCount) {
                    val tab: TabLayout.Tab = it.getTabAt(i) ?: continue
                    val tabView = tab.customView as TextView?
                    if (tab.position == position) {
                        tabView!!.textSize = activeSize
                        tabView.typeface = Typeface.DEFAULT_BOLD
                    } else {
                        tabView!!.textSize = normalSize
                        tabView.typeface = Typeface.DEFAULT
                    }
                }
            }
        }
    }

    override fun onDestroy() {
        mediator?.detach()
        viewPager2.unregisterOnPageChangeCallback(changeCallback)
        super.onDestroy()
    }
}